<template>
	<view class="">
		<view class="">
			
		</view>
		<view class="flex-row-space-between" style="height: 104rpx;background: #fff;">
			<view class="searchView flex1 flex-row-flex-start">
					<image class="searchImage" src="/static/common/search.png" mode="scaleToFill"></image>
					<input  v-model="keyword" placeholder="搜索关键字" @confirm="onSearch" class="searchInput flex1" type="text">
				</view>
				<view @click="onSearch" class="searchTitle">
					搜索
				</view>
		</view>
		<view v-if="!isShowHistory" class="" style="height: 2000rpx;background: #fff;padding:0 20rpx;">
			<view class="flex-row-space-between">
				<view class="historyTitle">
					历史搜索
				</view>
				<image @tap="clearSearchHistory" class="deleteImage" src="/static/lease/delete.png" mode="scaleToFill"></image>
			</view>
			<view class="flex-row-flex-start flex-wrap">
				<view v-for="(item,index) in historyList" :key="index">
					<view class="historyItem flex-column-center" @tap="onChangeKeyword(item)">
						{{item}}
					</view>
				</view>
			</view>
			
		</view>
		
		<view v-else style="margin-top: 20rpx;">
			<lease :list="goodsList"></lease>
		</view>
		
	</view>
</template>

<script>
    import { 
        getLeaseClassify,
        getLeaseGoodsList
    } from '@/api/lease.js';
    
	import {
		getSearchpage,
		clearSearch
	} from '@/api/store';
    
	import { loadingFun, formatPrice } from '@/utils/tools';
	import { loadingType } from '@/utils/type';


	export default {
		data() {
			return {
				isShowHistory:false,
				keyword: '',                    // 搜索内容
				status: loadingType.LOADING,    // 加载状态
				page: 1,                        // 当前数据页面
				goodsType: 'double',            // 商品列表样式展示方式
				goodsList: [],                  // 商品列表数据
				priceSort: '',                  // 价格排序
				saleSort: '',                   // 销量排序
                classify_id: '',                // 分类ID
				showHistory: false,             // 显示搜索历史
				historyList: [],                // 搜索历史数据列表
				hotList: [],                    // 热门搜素数据列表
                leaseClassifyList: [],          // 租赁分类列表
			};
		},
		watch: {
			// 监听属性
			keyword(value, old) {
				if (!value && !this.id){
					this.showHistory = true
				
				} 
			},
            // 是否处于搜索
			showHistory(value) {
				if (value) this.getSearchpageFun();
			}
		},
		computed: {
			comprehensive() {
                const priceSort = this.priceSort;
                const saleSort = this.saleSort;
                
                return (!priceSort && !saleSort) ? true : false;
			}
		},

		onLoad(options) {
            // // 获取分类列表数据
            // getLeaseClassify().then(res => {
            //     if (res.code != 1) return;
            //     this.leaseClassifyList = res.data;
            //     // this.classify_id = res.data[0].id || '';
            //     // 商品列表数据请求
            //     this.getGoodsList();
            // });
			
			 this.getSearchpageFun();
		},

        // 上拉加载更多数据
		onReachBottom() {
			this.getGoodsList();
		},
        
		methods: {
            // 格式化价格输出
            formatPrice,
            
            // 更改商品排序样式
			changeType() {
				this.goodsType = this.goodsType === 'one' ? 'double' : 'one'
			},

            // 清空搜索记录
			clearSearchHistory() {
				clearSearch().then(res => {
					if (res.code != 1) return;
                    this.getSearchpageFun();
				});
			},
			
			// 点击取消搜索
			onCancelSearch() {
				this.showHistory = false;
				// this.onRefresh();
			},
            
            // 点击分类
            onClassify(goods_id) {
                this.classify_id = goods_id;
                this.onRefresh();
            },

            // 点击综合
			onNormal() {
				this.priceSort = ''
				this.saleSort = ''
				this.onRefresh();
			},

            // 点击价格排序
			onPriceSort() {
				this.saleSort = ''
				this.priceSort = this.priceSort == 'asc' ? 'desc' : 'asc'
				this.onRefresh();
			},

            // 点击销量排序
			onSaleSort() {
				this.priceSort = ''
				this.saleSort = this.saleSort == 'desc' ? 'asc' : 'desc'
                this.onRefresh();
			},

            // 获取搜索变化
			getSearchpageFun() {
				getSearchpage().then(res => {
					if (res.code != 1) return;
                    const data = res.data;
					this.hotList = data.hot_lists
					this.historyList = data.history_lists
				});
			},
            
            // 搜索
			onSearch() {
				this.isShowHistory = true
				this.onRefresh();
			},
            
            // 刷新列表数据
			onRefresh() {
				this.showHistory = false
				this.page = 1
				this.goodsList = []
				this.status = loadingType.LOADING
				this.$nextTick(() => this.getGoodsList());
			},

            // 根据字符串搜索内容
			onChangeKeyword(item) {
				this.keyword = item;
				this.isShowHistory = true;
				this.onRefresh();
			},
            
            // 获取商品列表数据
            getGoodsList() {
            	if (this.status == loadingType.FINISHED) return;
                // 参数
                const field = {
                    category_id: this.classify_id,
                    keyword: this.keyword,
                    price: this.priceSort,
                    sales_sum: this.saleSort,
                }
                // 请求
            	loadingFun(
                  getLeaseGoodsList, 
                  this.page, 
                  this.goodsList, 
                  this.status, 
                  field
                ).then(res => {
                    console.log(res);
                    if (!res) return;
                    this.page = res.page;
                    this.goodsList = res.dataList;
                    this.status = res.status;
                });
            },
            
            // 查看商品详情
            goGoodsDetail(goods_id) {
                uni.navigateTo({
                    url: '/bundle/pages/lease_goods_details/lease_goods_details'
                      + '?' +'id=' + goods_id
                })
            }
		}
	};
</script>

<style scoped>
	page{
		background: #F6F9FB;
	}
	
.searchView{
	padding:  0 22rpx;
	height: 74rpx;
	margin: 0 20rpx;
	background: #F6F9FB;
	border-radius: 40rpx 40rpx 40rpx 40rpx;
	
}
.searchInput{
	background: #F6F9FB;
}
.searchImage{
	margin-right: 20rpx;
	width: 28rpx;
	height: 28rpx;
}
.searchTitle{
	text-align: right;
	width: 80rpx;
	margin-right: 30rpx;
	font-size: 27rpx;
	font-family: PingFang HK-Regular, PingFang HK;
	font-weight: 400;
	color: #FF4D40;
}
.historyTitle{
	margin-top: 40rpx;
	font-size: 31rpx;
	font-family: PingFang HK-Medium, PingFang HK;
	font-weight: 500;
	color: #333333;
}
.deleteImage{
	margin-top: 60rpx;
	width: 28rpx;
	height: 28rpx;
}
.historyItem{
	margin-right: 20rpx;
	margin-top: 20rpx;
	padding: 0 20rpx;
	height: 57rpx;
	background: #F6F9FB;
	border-radius: 8rpx 8rpx 8rpx 8rpx;
	opacity: 1;
}
</style>
